package com.Util;

import com.commont.DatabasePool;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.LoggerFactory;

import javax.inject.Singleton;
import javax.ws.rs.ext.Provider;
import java.sql.Connection;
@Singleton
public class DB {
    private static HikariDataSource dataSource;
    private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());

    public DB() {
        dbinit ();
    }

    public void dbinit(){
    log.info ( "【init db】" );
        try {
            Class.forName ( "com.mysql.jdbc.Driver" );
            DatabasePool Pooling = new DatabasePool();
            if (!Pooling.getStoragePooling()) {
                log.info("Pooling.getStoragePooling init error");
                return;
            }
            this.dataSource = Pooling.getDatabase();
        } catch (Exception e) {
            log.error ( "【初始化异常】={}",e );
        }
    }

    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static void quit(){
        dataSource.close();
    }

    public HikariDataSource getDataSource()
    {
        return this.dataSource;
    }

}
